package com.project.thisDatabase.base.model.data;

import com.project.annotation.data.Symbol;
import com.project.annotation.enums.data.SymbolCode;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
public class FieldItem {

    private Object[] value;
    private String key;
    private int type;

    public FieldItem(String key, int type, Object[] value) {
        this.key = key;
        this.type = type;
        this.value = value;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(key);
        sb.append(symbolString(type));
        if ((type & SymbolCode.IN) == type) {
            sb.append("(");
            StringBuilder in_val = new StringBuilder();
            Arrays.asList(value).forEach(f->{
                if (in_val.length() > 0) {
                    in_val.append(",");
                }
                in_val.append("?");
            });
            sb.append(in_val);
            sb.append(")");
        } else {
            sb.append("?");
        }

        if ((type & SymbolCode.NOT) == type) {
            return String.format("not(%s)", sb);
        }

        return sb.toString();
    }

    private String symbolString(int val) {
        StringBuilder syb = new StringBuilder();
        syb.append(" ");

        if ((val & SymbolCode.LT) == val) {
            syb.append("<");
        }

        if ((val & SymbolCode.GT) == val){
            syb.append(">");
        }

        if ((val & SymbolCode.EQUAL) == val) {
            syb.append("=");
        }

        if ((val & SymbolCode.LIKE) == val){
            syb.append("like");
        }

        if ((val & SymbolCode.IN) == val){
            syb.append("in");
        }
        syb.append(" ");
        return syb.toString();
    }
}
